package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.json.contactJson;
import com.example.yiwei.crm.json.customerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OtherBaseUpdateActivity extends AppCompatActivity {
    private EditText edit_bz;
    private Spinner spinner_rh,spinner_hy;
    private SharedPreferences sp;
    List<BZFLEntity> list;
    private String ylname;
    private TextView txt_next_time;
    private int ylid,fkid,hyid;
    private CustomerMessageEntity customer;
    String sresult;
    private TimePickerView mpv;
    ImageView im_comment,im_back;
    Button btn_show;
    List<String> fk_list,hy_list;
    private ArrayAdapter adapter5,adapter6;
    private String fk_name,hy_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_base_update);
        edit_bz = (EditText) findViewById(R.id.userAdd_edit_additional_remarks);
        spinner_rh = (Spinner) findViewById(R.id.add_customer_rufk);
        spinner_hy = (Spinner) findViewById(R.id.add_customer_hydj);
        btn_show= (Button) findViewById(R.id.customer_add_btn_show);
        im_back = (ImageView) findViewById(R.id.other_base_update_img_back);
        im_comment = (ImageView) findViewById(R.id.other_base_update_img_commit);
        txt_next_time = (TextView) findViewById(R.id.customer_add_next_time);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        customer = GetCustumer.get();
        im_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        im_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
        fk_list=new ArrayList<>();
        hy_list=new ArrayList<>();
        fk_list.add(0,"");
        fk_list.add(1,"未表态");
        fk_list.add(2,"糊涂");
        fk_list.add(3,"语言沟通障碍");
        fk_list.add(4,"未见面");
        fk_list.add(5,"无主见活动能力");
        fk_list.add(6,"拒绝沟通");
        hy_list.add(0,"");
        hy_list.add(1,"A");
        hy_list.add(2,"B");
        hy_list.add(3,"C");
        hy_list.add(4,"VIP");
        adapter5 = new ArrayAdapter(OtherBaseUpdateActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, fk_list);
        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner_rh.setAdapter(adapter5);
        adapter6 = new ArrayAdapter(OtherBaseUpdateActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, hy_list);
        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner_hy.setAdapter(adapter6);
        spinner_rh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                fkid = position;

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner_hy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                hyid = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        setMessage();
    }

    public void add(){
        im_comment.setVisibility(View.GONE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject json=sc.storage(sp);
                try {
                    json.put("KAA02",customer.getClient_name());
                    json.put("addoredit","edit");
                    json.put("KAA01",customer.getClient_id());
                    Log.d("cc",customer.getClient_id()+"");
                    json.put("CID03",sp.getString("CID03",null));
                    json.put("KAA34",fkid);
                    json.put("KAA37",hyid);
                    json.put("REK01",edit_bz.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String sjson=json.toString();
                Log.d("aa",sjson);
                //新增客户信息
                sresult = NetWorkUtils.getCount(baseClass.add_coor_customer_URL, sjson);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (sresult!=null){
                            try {
                                JSONObject object = new JSONObject(sresult);
                                boolean b = object.getBoolean("ifsuccess");
                                if (b){
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            JSONObject object1 = new JSONObject();
                                            try {
                                                object1.put("KAA01",customer.getClient_id());
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            GetCustumer.create();
                                            GetContact.create();
                                            String json = object1.toString();
                                            String reult = NetWorkUtils.getCount(baseClass.find_customer_msg, json);
                                            GetCustumer.customers = customerJson.jsonCustomerDetailedMessage1(reult,null);
                                            GetContact.contact = contactJson.detailedContactMessageJson1(reult,null);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (GetCustumer.customers!=null&& GetContact.contact!=null){
                                                        finish();
                                                    }

                                                }
                                            });
                                        }
                                    }).start();
                                }else {
                                    im_comment.setVisibility(View.VISIBLE);
                                   Toast.makeText(OtherBaseUpdateActivity.this, "添加客户失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        }).start();
    }
    public void setMessage(){
        spinner_hy.setSelection(customer.getHydj());
        spinner_rh.setSelection(customer.getRhfk());
        edit_bz.setText(customer.getAdditional_remarks());
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

}
